package cn.liumouren.jzxd.controller;

import cn.liumouren.jzxd.common.utils.PageUtils;
import cn.liumouren.jzxd.common.utils.R;
import cn.liumouren.jzxd.common.utils.ResConstant;
import cn.liumouren.jzxd.entity.Likes;
import cn.liumouren.jzxd.service.LikesService;
import cn.liumouren.jzxd.utils.QueryUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * @author Freeman Liu
 * @email llw599502537@gmail.com
 * @date 2020-10-31 00:44:46
 */
@RestController
@RequestMapping("/likes")
@Api(tags = "点赞")
public class LikesController {
    @Autowired
    private LikesService likesService;

    @ApiOperation("万能查询")
    @RequestMapping(value = "/customQuery", method = RequestMethod.GET)
    public R getSceneNames(@RequestParam Map<String, Object> map) {
        PageUtils result = QueryUtil.getResult(likesService, map);
        return R.ok("查询成功", result);
    }

    /**
     * 列表
     */
    @ApiOperation("分页查询")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = likesService.queryPage(params);

        return R.ok().put("data", page);
    }


    /**
     * 信息
     */
    @ApiOperation("根据id查询")
    @RequestMapping(value = "/info/{id}", method = RequestMethod.GET)
    public R info(@PathVariable("id") String id) {
        Likes likes = likesService.getById(id);

        return R.ok().put("data", likes);
    }

    /**
     * 保存
     */
    @ApiOperation("新增")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public R save(@RequestBody Likes likes) {
        likesService.save(likes);

        return R.ok();
    }

    /**
     * 修改
     */
    @ApiOperation("修改")
    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    public R update(@RequestBody Likes likes) {
        likesService.updateById(likes);

        return R.ok();
    }

    /**
     * 删除
     */
    @ApiOperation("批量删除")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public R delete(@RequestBody String[] ids) {
        likesService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    @ApiOperation("获取某条动态的所有点赞")
    @RequestMapping(value = "/getAllLikes/{zoneId}", method = RequestMethod.GET)
    public R delete(@PathVariable String zoneId) {
        List<Likes> likesList = likesService.getAllLikes(zoneId);

        return R.ok("成功获取动态[" + zoneId + "]的所有点赞", likesList);
    }

    @ApiOperation("获取某条动态点赞数")
    @RequestMapping(value = "/likesCount/{zoneId}", method = RequestMethod.GET)
    public R likesCount(@PathVariable String zoneId) {
        List<Likes> likesList = likesService.getAllLikes(zoneId);

        return R.ok("成功获取动态[" + zoneId + "]的点赞数", likesList.size());
    }

    @ApiOperation("查询用户是否给某一条动态点过赞")
    @RequestMapping(value = "/checkIsLiked/{zoneId}/{userId}", method = RequestMethod.GET)
    public R delete(@PathVariable String zoneId, @PathVariable String userId) {
        Likes likes = likesService.findByUserIdAndZoneId(zoneId, userId);

        if (likes == null) {
            return R.error(ResConstant.ERROR, "用户[" + userId + "]没有给动态[" + zoneId + "]点过赞");
        }
        return R.ok("用户[" + userId + "]给动态[" + zoneId + "]点过赞", likes);
    }

    @ApiOperation("取消点赞")
    @RequestMapping(value = "/notLikeNoMore/{zoneId}/{userId}", method = RequestMethod.DELETE)
    public R notLikeNoMore(@PathVariable String zoneId, @PathVariable String userId) {
        likesService.notLikeNoMore(zoneId, userId);
        return R.ok("用户[" + userId + "]取消给动态[" + zoneId + "]点赞");
    }
}
